package DP;

/**
 * @Classname JZ46把数字翻译成字符串
 * @Description TODO
 * @Date 2022/12/10 15:26
 * @Created by xjl
 */
public class JZ46把数字翻译成字符串 {

    public int translateNum(int num) {
        String s = String.valueOf(num);
        int a = 1, b = 1;
        for(int i = 2; i <= s.length(); i++) {
            // 判断两个数字
            String tmp = s.substring(i - 2, i);
            int c = tmp.compareTo("10") >= 0 && tmp.compareTo("25") <= 0 ? a + b : a;
            b = a;
            a = c;
        }
        return a;
    }

    public int translateNum2(int num) {
        char[] nums = String.valueOf(num).toCharArray();
        return slove(nums,0);
    }

    private int slove(char[] CharArray, int start) {
        //终端条件
        if (start >= CharArray.length) {
            return 1;
        }
        //考虑一个数字翻译为一种的是时候
        int res1 = slove(CharArray, start + 1);
        //两个翻译到一起的时候
        int res2 = 0;
        if ((start < CharArray.length - 1) && ((CharArray[start] - '0') * 10 + (CharArray[start + 1] - '0')>9&&(CharArray[start] - '0') * 10 + (CharArray[start + 1] - '0') < 26)) {
            res2 = slove(CharArray, start + 2);
        }
        return res1 + res2;
    }


    public int translateNum3(int num) {
        char[] chars = String.valueOf(num).toCharArray();
        int index=0;
        return next(chars,index);
    }

    private int next(char[] chars, int index) {
        if (index>chars.length-1){
            return 1;
        }
        int one= next(chars,index+1);
        int two=0;
        if (index<chars.length-1&&((chars[index] - '0') * 10 + (chars[index + 1] - '0')>9&&(chars[index] - '0') * 10 + (chars[index + 1] - '0') < 26)){
            two=next(chars,index+2);
        }
        return one+two;
    }
}
